import mongoose from 'mongoose'
import dayjs from 'dayjs'

const commentSchema = new mongoose.Schema(
  {
    comment: String,
    time: { type: Date, default: Date.now },
    blog: { type: mongoose.Schema.Types.ObjectId, ref: 'blog' },
    user: { type: mongoose.Schema.Types.ObjectId, ref: 'user' }
  },
  { versionKey: false }
)

// 虚拟字段：格式化时间
commentSchema.virtual('formattedTime').get(function () {
  return dayjs(this.time).format('YYYY-MM-DD HH:mm:ss')
})

// 开启虚拟字段的 JSON 输出
commentSchema.set('toJSON', { virtuals: true })
commentSchema.set('toObject', { virtuals: true })

export default commentSchema
